package switchviz.graphic.signal;

import java.util.Iterator;
import java.util.List;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JLabel;

import org.jdom.Element;

import switchviz.core.ResourceManager;
import switchviz.gui.component.JColorPanel;
import switchviz.gui.component.JLabeldComponent;
import switchviz.gui.component.JMaskedTextField;
import switchviz.gui.component.PaddingPanel;


public class ComplexSignalPanel extends PaddingPanel 
{


	/**
	 * 
	 */
	private static final String ADD = "Addition";
	private static final String SUB = "Subtraction";
	private static final String MUL = "Multiplication";
	private static final String DIV = "Division";
	
	private static final long serialVersionUID = 2130779159813407990L;
	private ComplexSignalPersistence _persistenceHandler = null;
	private ComplexSignalDataModel _datamodel = null;
	private JLabeldComponent _txtname = null;
	private JColorPanel _clsignal = null;
	private JLabel _lbltype = null;
	private JMaskedTextField _txtminvalue = null;
	private JMaskedTextField _txtmaxvalue = null;
	private JLabeldComponent _chbloweralarm = null;
	private JLabeldComponent _chbupperalarm = null;
	private JMaskedTextField _txtoffset = null;
	private JMaskedTextField _txtcoefficient = null;
	private JLabeldComponent _cmbaliases1 = null;
	private JLabeldComponent _cmbaliases2 = null;
	private JLabeldComponent _cmboperation = null;
	private JLabeldComponent _chblogarithm = null;
	private JLabeldComponent _cmbpriority = null;
	
	private List<String> _signals = null;
	
	public ComplexSignalPanel(Element node, List<String> signals) 
	{
		_persistenceHandler = new ComplexSignalPersistence(node);
		_signals = signals;
		initialize();
		this.setDataModel(_persistenceHandler.getDataModelFromElement());
	}
	
	private void initialize()
	{
		this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
		_txtname = new JLabeldComponent("Signal Name: ", JLabeldComponent.TEXTFIELD);
		_clsignal = new JColorPanel("Signal Color: ");
		_lbltype = new JLabel();
		_chblogarithm = new JLabeldComponent("Logarithm: ", JLabeldComponent.CHECKBOX);
		_cmbaliases1 = new JLabeldComponent("First Signal: ", JLabeldComponent.COMBOBOX);
		_cmbaliases2 = new JLabeldComponent("Second Signal: ", JLabeldComponent.COMBOBOX);
		
		Iterator<String> iter = _signals.iterator();
		
		_cmbaliases1.getComboBox().addItem("NONE");
		_cmbaliases2.getComboBox().addItem("NONE");
		
		while (iter.hasNext())
		{
			String name = iter.next();
			
			_cmbaliases1.getComboBox().addItem(name);
			_cmbaliases2.getComboBox().addItem(name);
		}
		
		_cmboperation = new JLabeldComponent("Operation: ", JLabeldComponent.COMBOBOX);
		_cmboperation.getComboBox().addItem(ADD);
		_cmboperation.getComboBox().addItem(SUB);
		_cmboperation.getComboBox().addItem(MUL);
		_cmboperation.getComboBox().addItem(DIV);
		
		_cmbpriority = new JLabeldComponent("Priority: ", JLabeldComponent.COMBOBOX);
		_cmbpriority.getComboBox().addItem("Simple");
		_cmbpriority.getComboBox().addItem("Detailed");
		_cmbpriority.getComboBox().addItem("Off");
		
		
		_txtoffset = new JMaskedTextField("Offset: ", JMaskedTextField.FLOATNUM);
		_txtcoefficient = new JMaskedTextField("Coefficient: ", JMaskedTextField.FLOATNUM);
		_txtminvalue = new JMaskedTextField("Minvalue: ", JMaskedTextField.FLOATNUM);
		_txtmaxvalue = new JMaskedTextField("Maxvalue: ", JMaskedTextField.FLOATNUM);
		_chbloweralarm = new JLabeldComponent("Lower Alarm: ", JLabeldComponent.CHECKBOX);
		_chbupperalarm = new JLabeldComponent("Upper Alarm: ", JLabeldComponent.CHECKBOX);
		this.add(_lbltype);
		this.add(_txtname);
		this.add(_cmbpriority);
		this.add(_clsignal);
		this.add(Box.createRigidArea(ResourceManager.getInstance().getConfigurationLabelDimension()));
		this.add(_cmbaliases1);
		this.add(_cmboperation);
		this.add(_cmbaliases2);
		this.add(_txtoffset);
		this.add(_txtcoefficient);
		this.add(_chblogarithm);
		this.add(Box.createRigidArea(ResourceManager.getInstance().getConfigurationLabelDimension()));
		this.add(_txtminvalue);
		this.add(_txtmaxvalue);
		this.add(_chbloweralarm);
		this.add(_chbupperalarm);
		
	}
	
	public Element getElement()
	{
		getDataModel().setColor(_clsignal.getColor());
		getDataModel().setName(_txtname.getText());
		getDataModel().setMin(Float.parseFloat(_txtminvalue.getText()));
		getDataModel().setMax(Float.parseFloat(_txtmaxvalue.getText()));
		getDataModel().setOffset(Float.parseFloat(_txtoffset.getText()));
		getDataModel().setCoefficient(Float.parseFloat(_txtcoefficient.getText()));
		getDataModel().setFirstalias(_cmbaliases1.getText());
		getDataModel().setSecondalias(_cmbaliases2.getText());
		getDataModel().setLogarithm(_chblogarithm.getCheckBox().isSelected());
		int alarmmode = 0;
		if (_chbloweralarm.getCheckBox().isSelected())
			alarmmode += BasicSignalDataModel.ALARM_LOWER_BOUND;
		if (_chbupperalarm.getCheckBox().isSelected())
			alarmmode += BasicSignalDataModel.ALARM_UPPER_BOUND;
		getDataModel().setAlarmmode(alarmmode);
		
		if (_cmboperation.getText().equals(ADD))
			getDataModel().setOperation(0);
		else if (_cmboperation.getText().equals(SUB))
			getDataModel().setOperation(1);
		else if (_cmboperation.getText().equals(MUL))
			getDataModel().setOperation(2);
		else if (_cmboperation.getText().equals(DIV))
			getDataModel().setOperation(3);
		
		if (_cmbpriority.getText().equals("Simple"))
			getDataModel().setPriority(0);
		else if (_cmbpriority.getText().equals("Detailed"))
			getDataModel().setPriority(1);
		else if (_cmbpriority.getText().equals("Off"))
			getDataModel().setPriority(2);
		
		return _persistenceHandler.setDataModel(getDataModel());
	}
	
	private ComplexSignalDataModel getDataModel()
	{
		if(_datamodel == null)
		{
			_datamodel = new ComplexSignalDataModel();
			_datamodel.setType(BasicSignalDataModel.TYPECOMPLEX);
		}
		return _datamodel;
	}

	private void setDataModel(ComplexSignalDataModel datamodel)
	{
		_datamodel = datamodel;
		_txtname.setText(_datamodel.getName());
		_clsignal.setColor(_datamodel.getColor());
		_lbltype.setText(_datamodel.getType());
		_txtminvalue.setText(String.valueOf(_datamodel.getMin()));
		_txtmaxvalue.setText(String.valueOf(_datamodel.getMax()));
		_txtoffset.setText(String.valueOf(_datamodel.getOffset()));
		_txtcoefficient.setText(String.valueOf(_datamodel.getCoefficient()));
		_cmbaliases1.setText(_datamodel.getFirstalias());
		_cmbaliases2.setText(_datamodel.getSecondalias());
		_chblogarithm.getCheckBox().setSelected(_datamodel.isLogarithm());
		
		switch (getDataModel().getOperation())
		{
		case 0:
			_cmboperation.setText(ADD);
			break;
		case 1:
			_cmboperation.setText(SUB);
			break;
		case 2:
			_cmboperation.setText(MUL);
			break;
		case 3:
			_cmboperation.setText(DIV);
			break;
		}
		
		switch (getDataModel().getPriority())
		{
		case 0:
			_cmbpriority.setText("Simple");
			break;
		case 1:
			_cmbpriority.setText("Detailed");
			break;
		case 2:
			_cmbpriority.setText("Off");
			break;
		}
		
		switch (getDataModel().getAlarmmode())
		{
		case BasicSignalDataModel.ALARM_NO_BOUND:
			_chbloweralarm.getCheckBox().setSelected(false);
			_chbupperalarm.getCheckBox().setSelected(false);
			break;
		case BasicSignalDataModel.ALARM_LOWER_BOUND:
			_chbloweralarm.getCheckBox().setSelected(true);
			_chbupperalarm.getCheckBox().setSelected(false);
			break;
		case BasicSignalDataModel.ALARM_UPPER_BOUND:
			_chbloweralarm.getCheckBox().setSelected(false);
			_chbupperalarm.getCheckBox().setSelected(true);
			break;
		case BasicSignalDataModel.ALARM_BOTH_BOUNDS:
			_chbloweralarm.getCheckBox().setSelected(true);
			_chbupperalarm.getCheckBox().setSelected(true);
			break;
		}
	}
}
